package elitech.vietnam.myfashion.dialog;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.config.Const;
import elitech.vietnam.myfashion.doinbackground.CallServiceTask;
import elitech.vietnam.myfashion.entites.Member;
import elitech.vietnam.myfashion.extend.AsyncTask;

public class UserInfoDialog extends Dialog implements View.OnClickListener {

	private MainActivity mContext;
	private TextView text_username, text_status;
	private Button button_close;
	private ImageView image_avatar;
	private int mMemberID;
	private Member mMember;

	public UserInfoDialog(MainActivity context, int memID) {
		super(context, R.style.Dialog);
		mContext = context;
		this.mMemberID = memID;
	}

	public UserInfoDialog(MainActivity context, Member member) {
		super(context, R.style.Dialog);
		mContext = context;
		this.mMember = member;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_userinfo);

		text_username = (TextView) findViewById(R.id.dialog_userinfo_text_name);
		text_status = (TextView) findViewById(R.id.dialog_userinfo_text_status);
		image_avatar = (ImageView) findViewById(R.id.dialog_userinfo_image_avatar);
		button_close = (Button) findViewById(R.id.dialog_userinfo_button_close);

		button_close.setOnClickListener(this);
		if (mMember == null)
			GetMember();
		else
			loadMember();
	}

	private void loadMember() {
		String name = mMember.Name + ((!mMember.NickName.equals("")) ? " (" + mMember.NickName + ")" : "");
		text_username.setText(name);
		text_status.setText(mMember.Profile);
		UrlImageViewHelper.setUrlDrawable(image_avatar, Const.SERVER_IMAGE_URL.replace("/app", "") + mMember.Image, R.drawable.no_avatar);
	}

	@Override
	public void onClick(View arg0) {
		if (arg0.getId() == R.id.dialog_userinfo_button_close)
			dismiss();
	}

	private void GetMember() {
		new CallServiceTask() {
			@Override
			public void work() {
				int accountID = (mContext.mUser == null) ? -1 : mContext.mUser.Id;
				try {
					mMember = service.getInfoMemberByID(mMemberID, accountID);
				} catch (IOException e) {
					e.printStackTrace();
				} catch (XmlPullParserException e) {
					e.printStackTrace();
				}
			}

			protected void onPostExecute(Void result) {
				if (mMember == null) {
					Toast.makeText(mContext, mContext.getString(R.string.connecterror), Toast.LENGTH_SHORT).show();
					dismiss();
				} else
					loadMember();
			};
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}
}
